package com.kykj.kykj.controller;

import com.kykj.kykj.entity.ArticlesEntity;
import com.kykj.kykj.entity.ResultEntity;
import com.kykj.kykj.entity.RoleEntity;
import com.kykj.kykj.entity.UserEntity;
import com.kykj.kykj.service.ArticlesService;
import com.kykj.kykj.service.impl.ArticlesServiceImpl;
import com.kykj.kykj.tool.KoDoTool;
import com.kykj.kykj.vo.ArticlesVo;
import com.kykj.kykj.vo.UserVo;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.UUID;

@RequestMapping("v2/articles/")
@CrossOrigin//解决跨域问题
@RestController
public class ArticlesController {
    @Resource
    ArticlesServiceImpl articlesService;

    @Value("${project.localfilepath}")
    private String localFilePath;//本地保存文件路径

    @Resource
    KoDoTool koDoTool;//注入KodDoTool
    /**
     * 获取所有用户列表
     * @return resultEntity
     */
    @GetMapping("getArticleslist")
    private ResultEntity getUserList(){
        ResultEntity resultEntity = new ResultEntity();
        try{
            resultEntity.setData(articlesService.getArticlesList());
            for (ArticlesEntity list:articlesService.getArticlesList()){
                System.out.println(list.getUserId());
            }
            resultEntity.setCode(0);
            resultEntity.setMsg("数据获取成功！");
        }catch (Exception e){
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return  resultEntity;
    }

    /**
     * 增加文章
     * @param articlesVo 文章对象
     * @return resultEntity
     */
    @PostMapping("articles")
    private ResultEntity addArticle(@RequestBody ArticlesVo articlesVo){
        ResultEntity resultEntity = new ResultEntity();
        try{
            resultEntity.setCode(articlesService.addArticle(articlesVo)>0?0:1);
            if(articlesService.addArticle(articlesVo)>0){
                System.out.println("当前自增的id是"+articlesVo.getId()+"------自增");
                resultEntity.setCode(0);
                resultEntity.setMsg("添加成功！");
            }else{
                resultEntity.setCode(1);
                resultEntity.setMsg("添加失败！"+articlesVo.getId());
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return  resultEntity;
    }

    /**
     * 根据id删除文章
     * @param id 文章id
     * @return resultEntity
     */
    @DeleteMapping("articles/{id}")
    private ResultEntity delArticle(@PathVariable("id") int id){
        ResultEntity resultEntity = new ResultEntity();
        try{
            if(articlesService.delArticle(id)>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("删除成功！");
            }else{
                resultEntity.setCode(1);
                resultEntity.setMsg("删除失败！");
            }
        }catch (Exception e){
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return  resultEntity;
    }

    /**
     * 修改文章
     * @param articlesVo 文章对象
     * @return resultEntity
     */
    @PutMapping("articles")
    private ResultEntity upArticle(@RequestBody ArticlesVo articlesVo){
        ResultEntity resultEntity = new ResultEntity();
        try{
            if(articlesService.upArticle(articlesVo)>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("修改成功！");
            }else{
                resultEntity.setCode(1);
                resultEntity.setMsg("修改失败！");
            }
        }catch (Exception e){
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return  resultEntity;
    }

    /**
     * 根据标题和标签获取分页获取内容
     * @param title 标题
     * @param tags 标签
     * @param page 分页
     * @param limit 每页行数
     * @return
     */
    @GetMapping("getArticleByfieId")
    private ResultEntity getArticleByfieId(
            @RequestParam("title") String title,
            @RequestParam("tags") String tags,
            @RequestParam("page") int page,
            @RequestParam("limit") int limit
    ){
        ResultEntity resultEntity = new ResultEntity();
        try{
            int start =(page-1)*limit;
            List<ArticlesEntity> list= articlesService.getArticleByfieId(title, tags, start, limit);
            if(!title.equals("")||!tags.equals("")){
                resultEntity.setCount(list.size());
            }else{
                resultEntity.setCount(articlesService.getArticlesCount());
            }
            resultEntity.setData(list);
            resultEntity.setMsg("数据获取成功");
            resultEntity.setCode(0);
        }catch (Exception exception){
            exception.printStackTrace();
            resultEntity.setCode(-9999);
            resultEntity.setMsg("数据获取失败！");
        }
        return resultEntity;
    }

    /**
     * 上传封面
     * @param multipartFile 封面图片
     * @param id 文章id
     * @return resultEntity
     */
    @PostMapping("upCover")
    private ResultEntity upCover(@RequestParam("cover") MultipartFile multipartFile, @RequestParam("id") int id ){
        ResultEntity resultEntity=new ResultEntity();
        if(! new File(localFilePath).exists()){//如果文件夹不存在
            if(! new File(localFilePath).mkdir()){
                resultEntity.setCode(3);
                resultEntity.setMsg("文件夹创建失败！！");
                return resultEntity;
            }
        }
        String sufName=multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));//获取文件后缀名后缀名
        String fileName= UUID.randomUUID().toString()+sufName;//生成新的文件名
        String pathFileName=localFilePath+fileName;//本地文件保存的全路径
        File dest=new File(pathFileName);//生成文件对象
        try {
            multipartFile.transferTo(dest);//保存文件
            String KoDoUrl=koDoTool.UpLoadFile(pathFileName);//上传文件到七牛云
            if (koDoTool==null){
                resultEntity.setCode(-1);
                resultEntity.setMsg("文件上传云端失败！");
            }
            else {
                if (articlesService.upCover(KoDoUrl,id)==0){
                    resultEntity.setCode(-2);
                    resultEntity.setMsg("文件上传数据库失败！");
                }else {
                    resultEntity.setCode(0);
                    resultEntity.setMsg("头像上传成功！");

                }
            }
        } catch (Exception e) {
            resultEntity.setCode(-9999);
            resultEntity.setMsg("文件上传失败！");
            e.printStackTrace();
        }
        return resultEntity;
    }

    /**
     * 根据用户id去获取文章
     * @param userId 用户id
     * @return resultEntity
     */
    @GetMapping("getArticlesByUserid")
    private ResultEntity getNextReplyList(
            @RequestParam("userId") int userId
    ){
        ResultEntity resultEntity = new ResultEntity();
        try{
            resultEntity.setData(articlesService.getArticlesByUserid(userId));
            resultEntity.setCode(0);
            resultEntity.setMsg("数据获取成功！");
        }catch (Exception e){
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return  resultEntity;
    }
}
